package command

import (
	"log"
	"strconv"
	"strings"
)

type ArgMap map[string]string

func (a ArgMap) String(name string) string {
	return a[name]
}

func (a ArgMap) Int64(name string) int64 {
	userId, err := strconv.ParseInt(a[name], 10, 64)
	if err != nil {
		log.Printf("format args error %v", err)
	}
	return userId
}

type Args struct {
	list []string
}

func (a *Args) Register(name string) {
	a.list = append(a.list, name)
}

func (a *Args) parse(input string) ArgMap {
	params := strings.Split(input, " ")
	if len(a.list) > len(params)-1 {
		return nil
	}
	argMap := make(ArgMap)
	for i := 0; i < len(a.list); i++ {
		params = params[1:]
		argMap[a.list[i]] = params[0]
	}
	return argMap
}
